#!/bin/sh

# Slackware build script for Qt 4
# Written by Aleksandar Samardzic <asamardzic@gmail.com>
# Modified by Robby Workman <rworkman@slackbuilds.org>
# Modified by Heinz Wiesinger <pprkut@liwjatan.at>

PRGNAM=qt4
VERSION=${VERSION:-4.5.1}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

if [ "${RASTER:-no}" = "no" ]; then
  GRAPHICS=""
else
  GRAPHICS="-graphicssystem raster"
fi

if [ "${PHONON:-yes}" = "yes" ]; then
  MEDIA="-phonon"
else
  MEDIA="-no-phonon"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf qt-x11-opensource-src-$VERSION
tar xvf $CWD/qt-x11-opensource-src-$VERSION.tar.bz2
cd qt-x11-opensource-src-$VERSION
chown -R root:root .
find . \
 \( -perm 777 -o -perm 775 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

if ! [ "${QTCOPY:-no}" = "no" ]; then
  patch -p0 -i $CWD/patches/0180-window-role.diff
  patch -p0 -i $CWD/patches/0195-compositing-properties.diff
  patch -p0 -i $CWD/patches/0225-invalidate-tabbar-geometry-on-refresh.patch
  patch -p0 -i $CWD/patches/0234-fix-mysql-threaded.diff
  patch -p0 -i $CWD/patches/0255-qtreeview-selection-columns-hidden.diff
  patch -p0 -i $CWD/patches/0274-shm-native-image-fix.diff
  patch -p0 -i $CWD/patches/0279-svg-rendering-regression.diff
  patch -p0 -i $CWD/patches/0280-deserialization-custom-dbus-properties.diff
fi

# The following two patches are taken from the qt.SlackBuild in -current
if [ $ARCH = "i486" ]; then
  patch -p1 -i $CWD/patches/qt.x86.cflags.diff
fi

# Fix path to mysql header
patch -p1 -i $CWD/patches/qt.mysql.h.diff

export CFLAGS="$SLKCFLAGS"
export CXXFLAGS="$SLKCFLAGS"
export OPENSOURCE_CXXFLAGS="$SLKCFLAGS"
echo $'o\nyes' | ./configure \
  -prefix /usr/lib/qt4 \
  -no-separate-debug-info \
  -nomake examples \
  -nomake demos \
  -qt-gif \
  -fast \
  -system-libpng \
  -system-libjpeg \
  -system-zlib \
  -dbus \
  -I/usr/include/mysql -L/usr/lib/mysql -R/usr/lib/mysql -plugin-sql-mysql \
  -webkit \
  -silent \
  $GRAPHICS \
  $MEDIA

make
make install INSTALL_ROOT=$PKG

( cd $PKG/usr/lib ; ln -sf qt4 qt-$VERSION )

# Qt4 uses different library names than Qt3 so this should be safe
# Link the shared libraries into /usr/lib:
( cd $PKG/usr/lib
  for file in qt4/lib/*.so* ; do
    ln -sf $file .
  done
)

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  for file in assistant assistant_adp designer lconvert linguist lrelease \
    lupdate moc pixeltool qcollectiongenerator qdbus qdbuscpp2xml qdbusviewer \
    qdbusxml2cpp qhelpconverter qhelpgenerator qmake qt3to4 qtconfig rcc uic \
    uic3 xmlpatterns;
  do
    ln -sf /usr/lib/qt4/bin/$file ${file}-qt4 ;
  done
)

mkdir -p $PKG/usr/lib/pkgconfig
( cd $PKG/usr/lib/pkgconfig
  for file in phonon.pc Qt3Support.pc QtAssistantClient.pc QtCLucene.pc \
    QtCore.pc QtDBus.pc QtDesigner.pc QtDesignerComponents QtGui.pc \
    QtHelp.pc QtNetwork.pc QtOpenGL.pc QtScript.pc QtScriptTools.pc QtSql.pc \
    QtSvg.pc QtTest.pc QtUiTools.pc QtWebKit.pc QtXml.pc QtXmlPatterns.pc ;
  do
    ln -sf /usr/lib/qt4/lib/pkgconfig/$file . ;
  done
)

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
cp -a LGPL_EXCEPTION.txt INSTALL LICENSE.GPL* KNOWN.ISSUES \
  README changes-$VERSION \
  $PKG/usr/doc/$PRGNAM-$VERSION
( cd $PKG/usr/doc/$PRGNAM-$VERSION ; ln -sf /usr/lib/qt4/doc/html . )

cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
